/**
 * *******************************************
 * user: zmq
 * describe: gate服务器 负责所有数据库的增删改查操作
 * created:
 * date: 2021/8/18
 * *******************************************
 */

import {Application} from 'pinus';
import Utils from "../../../util/utils";

export default function (app: Application) {
    return new DaoRemote(app);
}

export class DaoRemote {

    constructor(private app: Application) {
        this.app = app;
    }

    /**
     * 执行数据库语句
     * @param sql
     * @param args
     * @returns {Promise<any>}
     */
    public async query(sql: string, args: []): Promise<any> {

        let dbClient = this.app.get('dbClient');                                // 获取全局mysql client
        // return Utils.promisefy(dbClient.query.bind(dbClient), sql, args);       // 返回一个promise对象
        return dbClient.query(sql, args);

    }

    /**
     * 执行数据库语句
     * @param sql
     * @param args
     * @returns {Promise<any>}
     */
    public async doServerQuery(sql: string, args: []): Promise<any> {

        let serverDBClient = this.app.get('serverDBClient');                                // 获取全局mysql client
        return  Utils.promisefy(serverDBClient.query2.bind(serverDBClient), sql, args);

    }

}
